/*****************************************************************************************************************************
 * Program:	Army Engine 
 * Purpose:	Allows you to control every aspects of armory stuffs that exists in Black and White 2 
 *          which includes; change your platoons to other tribes platoons, change the mesh of the soldiers, change the weapons
 *          of the soldiers, change the formations of the platoons and so on...
 * Author:		Egerion 
 * Version: 	1.0
 * Date: 		August 26 2019 11:12
******************************************************************************************************************************/
//-------------------------------------------------Scripts---------------------------------------------------------------------

//script defines 
define script RunScripts 
define script PlayerPlatoonProperties
define script PlayerPlatoonFormation 

//global constants 
/*
 there are two keyboard keys that allows you to control your armies (or enemy if you want to)
 you can replace these keys by changing the global constants.

 ex: KB_U means: key "U", KB_T means: key "T"
*/
global constant MasterSelectionKey = KB_U  
global constant MasterKey = KB_V

//global variables 
global PlayerPlatoon = 0
global PlayerPlatoonSize = 0
global PlayerHandPos = 0
global ArmyFlag = 0
global SoldierCounter = 0
global SoldierDude = 0
global NewSoldierDude = 0
global PlayerID = 0 //integer equals the players id, zero is equals to player
global XP = 0
global NewPlayerPlatoon = 0
global MasterOption = 0

run script RunScripts 
begin script RunScripts
start 
    /*
    you can add and run your custom scripts from here
    */
    run background script PlayerPlatoonFormation
    run background script PlayerPlatoonProperties

end script RunScripts


begin script PlayerPlatoonProperties

    SoldierArr[1000]

    PlatoonTypeRangedNumber = 10
    PlatoonSoldierTypeNumber = 0
    PlatoonTypeMeleeNumber = 0
    CarriedObjectNumber = 0
    CurrExperience = 0
    PlatoonPos = 0
    PlatoonSpeed = 0

    WeaponClickCnt = 0 
    MeshClickCnt = 0 
    FlagClickCnt = 0
    ArrCnt = 0
    XpCnt = 0
    ScaleCnt = 1 //scale should start at 1

start
    begin loop 
        
        PlayerHandPos = marker at hand position //locate the player hand 
        PlayerPlatoon = get platoon of player PlayerID nearest {PlayerHandPos} radius 50 //find the platoon 
        ArmyFlag = get SCRIPT_OBJECT_TYPE_ARMY_FLAG at {PlayerPlatoon} radius 50  //find the army flag
        
        //master option section, you can add or remove options over here 
        if(key MasterSelectionKey down)
            if(MasterOption == 0) //change the tribe of the platoon 1
                begin dialogue
                say "Platoon Tribe Type Change Option Selected*"
                wait 0.65 seconds 
                end dialogue 
            elsif(MasterOption == 1) //change the soldiers carried object 2
                begin dialogue
                say "Soldier's Carried Object Change Option Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 1)
                begin dialogue
                say "Platoon Carried Object Change Option Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 2) //change the formation of the platoon 3
                begin dialogue
                say "Platoon's Formation Type Change Option Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 3) //change the mesh of the soldiers 4
                begin dialogue
                say "Soldiers Mesh Change Option Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 4) //change the soldiers of the platoon 5
                begin dialogue
                say "Soldier Change of Platoon Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 5) //change the experience of the platoon 6
                begin dialogue
                say "Experience Change of The Platoon Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 6) //scale change of the platoon 7
                begin dialogue
                say "Scale Change of The Platoon Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 7) //speed change of the platoon 8
                begin dialogue
                say "Speed Change of The Platoon Selected*"
                wait 0.65 seconds
                end dialogue 
            elsif(MasterOption == 8) //turn back to default settings
                MasterOption = -1
            end if 
            MasterOption++ 
        end if 

//-------------------------------------------------Controlling The Flags---------------------------------------------------------------------
        if(ArmyFlag left clicked) 
            if(platoon PlayerPlatoon melee) and (FlagClickCnt == 0)
                override mesh for ArmyFlag with "..\models\m_banneraztecmelee" //change the flag model 
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon melee) and (FlagClickCnt == 1)
                override mesh for ArmyFlag with "..\models\m_bannernorsemelee"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon melee) and (FlagClickCnt == 2)
                override mesh for ArmyFlag with "..\models\m_bannerjapanesemelee"
                clear left clicked object
                FlagClickCnt++

            elsif(platoon PlayerPlatoon melee) and (FlagClickCnt == 3)
                override mesh for ArmyFlag with "..\models\m_bannergreekmelee"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon melee) and (FlagClickCnt == 4)
                override mesh for ArmyFlag with "..\models\m_bannergeneric"
                clear left clicked object
                FlagClickCnt = 0 //set to default
            elsif(platoon PlayerPlatoon ranged) and (FlagClickCnt == 0)
                override mesh for ArmyFlag with "..\models\m_banneraztecranged"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon ranged) and (FlagClickCnt == 1)
                override mesh for ArmyFlag with "..\models\m_bannernorseranged"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon ranged) and (FlagClickCnt == 2)
                override mesh for ArmyFlag with "..\models\m_bannerjapaneseranged"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon ranged) and (FlagClickCnt == 3)
                override mesh for ArmyFlag with "..\models\m_bannergreekranged"
                clear left clicked object
                FlagClickCnt++
            elsif(platoon PlayerPlatoon ranged) and (FlagClickCnt == 4)
                override mesh for ArmyFlag with "..\models\m_bannergeneric"
                clear left clicked object
                FlagClickCnt = 0 //set to default
            end if 
        end if 

//-------------------------------------------------Change The Mesh OF The Platoons---------------------------------------------------------------------

        //i didnt add all the human models in the game, but most of them are here, you can add more by yourself.
        if(key MasterKey down) and (PlayerPlatoon exists) and (MasterOption == 4)

            PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
            //change the mesh of the soldiers
            force while SoldierCounter < PlayerPlatoonSize
                SoldierDude = get platoon PlayerPlatoon member at offset SoldierCounter
                if(MeshClickCnt == 0)
                    override mesh for SoldierDude with "..\skins\s_norsewarriore"
                elsif(MeshClickCnt == 1)
                    override mesh for SoldierDude with "..\skins\s_norserobinhood"
                elsif(MeshClickCnt == 2)
                    override mesh for SoldierDude with "..\skins\s_norsesigved"
                elsif(MeshClickCnt == 3)
                    override mesh for SoldierDude with "..\skins\s_norsepriestfenrick"
                elsif(MeshClickCnt == 4)
                    override mesh for SoldierDude with "..\skins\s_norsepriestfenrick_stripped"
                elsif(MeshClickCnt == 5)
                    override mesh for SoldierDude with "..\skins\s_norsemerrymen06"
                elsif(MeshClickCnt == 6)
                    override mesh for SoldierDude with "..\skins\s_norsemerrymen07"
                elsif(MeshClickCnt == 7)
                    override mesh for SoldierDude with "..\skins\s_norsemerrymen08"
                elsif(MeshClickCnt == 8)
                   override mesh for SoldierDude with "..\skins\s_norsemerrymen09"
                elsif(MeshClickCnt == 9)
                    override mesh for SoldierDude with "..\skins\s_skeletonghost"
                elsif(MeshClickCnt == 10)
                    override mesh for SoldierDude with "..\skins\s_japanwarriord"
                elsif(MeshClickCnt == 11)
                    override mesh for SoldierDude with "..\skins\s_japanyuka"
                elsif(MeshClickCnt == 12)
                    override mesh for SoldierDude with "..\skins\s_japanhiroku"
                elsif(MeshClickCnt == 13)
                    override mesh for SoldierDude with "..\skins\s_japantaranaga"
                elsif(MeshClickCnt == 14)
                    override mesh for SoldierDude with "..\skins\s_japanarcherc"
                elsif(MeshClickCnt == 15)
                    override mesh for SoldierDude with "..\skins\s_japanboatmaster"
                elsif(MeshClickCnt == 16)
                    override mesh for SoldierDude with "..\skins\s_japanboy"
                elsif(MeshClickCnt == 17)
                    override mesh for SoldierDude with "..\skins\s_aztecoilbaron"
                elsif(MeshClickCnt == 18)
                    override mesh for SoldierDude with "..\skins\s_aztecpriesttezozomoc"
                elsif(MeshClickCnt == 19)
                    override mesh for SoldierDude with "..\skins\s_egyptpriestesssanura"
                elsif(MeshClickCnt == 20)
                    override mesh for SoldierDude with "..\skins\s_egyptslavedisguise"
                elsif(MeshClickCnt == 21)
                    override mesh for SoldierDude with "..\skins\s_egyptassassin"
                elsif(MeshClickCnt == 22)
                    override mesh for SoldierDude with "..\skins\s_egyptbazmet"
                elsif(MeshClickCnt == 23)
                    override mesh for SoldierDude with "..\skins\s_nymph"
                elsif(MeshClickCnt == 24)
                    override mesh for SoldierDude with "..\skins\s_norserobinhood"
                elsif(MeshClickCnt == 25)
                    override mesh for SoldierDude with "..\skins\s_norsesigved"
                elsif(MeshClickCnt == 26)
                    override mesh for SoldierDude with "..\skins\s_rabbit"
                elsif(MeshClickCnt == 27)
                    override mesh for SoldierDude with "..\skins\s_shark"
                elsif(MeshClickCnt == 28)
                    override mesh for SoldierDude with "..\skins\s_swallow"
                elsif(MeshClickCnt == 29)
                    override mesh for SoldierDude with "..\skins\s_wolf"
                elsif(MeshClickCnt == 30)
                    override mesh for SoldierDude with "..\skins\s_cattle"
                end if 
                SoldierCounter++ //look another soldier 
            end while 
            MeshClickCnt++ //increment the type counter 
            SoldierCounter = 0 //set to default settings 

            if(MeshClickCnt >= 30) //to dont exceed counter amount
                MeshClickCnt = 0
            end if 

        end if 

//-------------------------------------------------Change The Platoon Type---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (MasterOption == 1)

            if(PLAYER of PlayerPlatoon == 0) and (PlayerPlatoon exists) and (platoon PlayerPlatoon melee) and (not PlayerPlatoon recruiting)
                PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
                XP = get platoon PlayerPlatoon experience //detect the current xp of platoon
                PlatoonPos = marker at {PlayerPlatoon} //mark the last position of platoon 
                delete PlayerPlatoon

                NewPlayerPlatoon = create platoon constant PlatoonTypeMeleeNumber at {PlatoonPos} with PlayerPlatoonSize men and 0 women
                set platoon NewPlayerPlatoon experience to XP //add the current xp 
                PLAYER of NewPlayerPlatoon = 0
                /*
                i've skipped some platoons that exists in the game like; Norse Reinforcement and Monk Reinforcement
                but you can add them by modifying this statements down below. Check the numerical values of these platoons
                over platoonInfoEnum
                */
                 if(PlatoonTypeMeleeNumber < 100) //ghost platoon
                    PlatoonTypeMeleeNumber = PlatoonTypeMeleeNumber + 20
                elsif(PlatoonTypeMeleeNumber == 100)
                    PlatoonTypeMeleeNumber = PlatoonTypeMeleeNumber + 1
                elsif(PlatoonTypeMeleeNumber == 101) //marauder platoon type 
                    PlatoonTypeMeleeNumber = 103
                elsif(PlatoonTypeMeleeNumber == 103) //monk platoon type 
                    PlatoonTypeMeleeNumber = 0
                end if  

            elsif(PLAYER of PlayerPlatoon == 0) and (PlayerPlatoon exists) and (platoon PlayerPlatoon ranged) and (not PlayerPlatoon recruiting)

                PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
                XP = get platoon PlayerPlatoon experience //detect the current xp of platoon
                PlatoonPos = marker at {PlayerPlatoon} //mark the last position of platoon 
                delete PlayerPlatoon

                NewPlayerPlatoon = create platoon constant PlatoonTypeRangedNumber at {PlatoonPos} with PlayerPlatoonSize men and 0 women
                set platoon NewPlayerPlatoon experience to XP //add the current xp 
                PLAYER of NewPlayerPlatoon = 0

                PlatoonTypeRangedNumber = PlatoonTypeRangedNumber + 20
                if(PlatoonTypeRangedNumber > 90)
                    PlatoonTypeRangedNumber = 10
                end if 
                wait until (NewPlayerPlatoon exists)
            end if 
        end if 

//-------------------------------------------------Controlling The Weapons Of The Soldiers---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (MasterOption == 2)
            PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
            //change the carried objects
            force while SoldierCounter < PlayerPlatoonSize
                SoldierDude = get platoon PlayerPlatoon member at offset SoldierCounter
                set SoldierDude carrying constant CarriedObjectNumber
                SoldierCounter++ //look another soldier 
            end while 
            CarriedObjectNumber++ //increment to counter to switch back to other carried object types 
            SoldierCounter = 0 //set to default settings

            if(CarriedObjectNumber >= 44) //there are maximum 44 carried object types 
                CarriedObjectNumber = 0 //turn back to initial carried object
            end if 
            
        end if 

//-------------------------------------------------Change The Soldiers Of The Platoons---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (MasterOption == 5)
            wait 1.5 seconds
            PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
            force while SoldierCounter < PlayerPlatoonSize 
                SoldierDude = get platoon PlayerPlatoon member at offset SoldierCounter
                SoldierArr[ArrCnt] = SoldierDude
                SoldierCounter++
                ArrCnt++
            end while 
            SoldierCounter = 0 
            force while SoldierCounter < PlayerPlatoonSize
                SoldierDude = get platoon PlayerPlatoon member at offset SoldierCounter
                NewSoldierDude = create SOLDIER constant PlatoonSoldierTypeNumber at {SoldierDude} //create new soldier 
                delete SoldierArr[ArrCnt]
                add villager NewSoldierDude to platoon PlayerPlatoon  //add new soldier into platoon
                SoldierCounter++ //increment the soldier counter to look another offset
                ArrCnt-- 
            end while 
            delete SoldierArr[ArrCnt]
            ArrCnt--
            SoldierCounter = 0 //set counter to zero    

            //loop back to aztec soldier and increment it all the way up to norse ghost soldiers
            PlatoonSoldierTypeNumber = (PlatoonSoldierTypeNumber + 10) 
            if(PlatoonSoldierTypeNumber > 110)
                PlatoonSoldierTypeNumber = 0
            end if 
            wait 1.5 seconds 
        end if 

//-------------------------------------------------Change The Experience Level Of The Platoons---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (not PlayerPlatoon recruiting) and (MasterOption == 6) 
            begin loop
                increment platoon PlayerPlatoon experience
                CurrExperience = get platoon PlayerPlatoon experience

                until CurrExperience >= XpCnt
            end loop

            XpCnt++ //increment the xp counter
            if(XpCnt >= 10) //if exceeds the max xp level, set back to zero 
                XpCnt = 0
            end if 

            begin dialogue
                say "Your Platoon's Xp has Increased to $d*" with number (XpCnt)
                wait 0.65 seconds
            end dialogue
        end if 

//-------------------------------------------------Change The Height Of The Soldiers---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (not PlayerPlatoon recruiting) and (MasterOption == 7)
            PlayerPlatoonSize = get number of villagers in platoon PlayerPlatoon //detect the size of the platoon
            force while SoldierCounter < PlayerPlatoonSize
                    SoldierDude = get platoon PlayerPlatoon member at offset SoldierCounter
                    SCALE of SoldierDude = ScaleCnt
                    SoldierCounter++ //look another soldier 
            end while 
        
            ScaleCnt = (ScaleCnt + 0.1)
            SoldierCounter = 0 //set to default settings

            if(ScaleCnt >= 5)
                ScaleCnt = 0
            end if 
            
        end if 

//-------------------------------------------------Change The Speed Of The Soldiers---------------------------------------------------------------------
        if(key MasterKey down) and (PlayerPlatoon exists) and (not PlayerPlatoon recruiting) and (MasterOption == 8)
            set platoon movement value FORMATION_MOVEMENT_STAT_PLATOON_SPEED of PlayerPlatoon to PlatoonSpeed
            PlatoonSpeed = (PlatoonSpeed + 0.5) //increment the speed 
            
            if(PlatoonSpeed >= 21) //max value that platoon's speed can reach 
                PlatoonSpeed = 0
            end if 

        end if 

    end loop 
        
end script PlayerPlatoonProperties


begin script PlayerPlatoonFormation

    FormationCnt = 0

start
    /*
    formation types should always stay inside the loop, if not; your platoon turn back to its default formation
    after 3 seconds. Formation types are hardcodded to your platoon's tribe type.
    */
    begin loop
        PlayerHandPos = marker at hand position //locate the player hand 
        PlayerPlatoon = get platoon of player PlayerID nearest {PlayerHandPos} radius 900000 //find the platoon 

        if(key MasterKey down) and (PlayerPlatoon exists) and (MasterOption == 3)
            FormationCnt++
            if(FormationCnt >= 7) //if formation count is equals to 5, it will turn back to platoons default formation after 3 seconds
                FormationCnt = 0 
            end if 
        end if 
        
        if(FormationCnt == 1)
            set army PlayerPlatoon formation to FORMATION_TYPE_GRID
        elsif(FormationCnt == 2)
            set army PlayerPlatoon formation to FORMATION_TYPE_OFFSET_GRID    
        elsif(FormationCnt == 3)
            set army PlayerPlatoon formation to FORMATION_TYPE_RABBLE     
        elsif(FormationCnt == 4)
            set army PlayerPlatoon formation to FORMATION_TYPE_CIRCLE   
        elsif(FormationCnt == 5)
            set army PlayerPlatoon formation to FORMATION_TYPE_UNTIDY_GRID       
        elsif(FormationCnt == 6)
            FormationCnt++
        end if 
    end loop
end script PlayerPlatoonFormation